package safeCommitter.core.diffrunbridge;

import java.util.Set;

import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jface.action.IAction;
import org.tigris.subversion.subclipse.ui.operations.CommitOperation;

import safeCommitter.core.progress.TestSearcherProgressHandler;
import safeCommitter.model.adpters.ClassEntity;
import safeCommitter.model.adpters.ISCTest;
import safeCommitter.test.finder.simple.SCSimpleTestFinder;

/**
 * 
 * Act as a bridge connecting the search and executors mechanism of the plugin.
 * Knows which clazz needs to be called in order to launch the tests.
 * 
 * @author Luiz Henrique Rolim
 */
public class SearchExecutorBridge implements IExecutorBridge {

	private final RunTestPhaseStarter runTestPhaseStarter = new RunTestPhaseStarter();

	public void executeTests(final IAction action,
			final IJavaProject javaProject,
			final Set<ClassEntity> clazzEntities, final CommitOperation c) {
		try {
			SCSimpleTestFinder finder = new SCSimpleTestFinder(javaProject,
					clazzEntities);
			TestSearcherProgressHandler progressHandler = new TestSearcherProgressHandler(
					finder);
			Set<ISCTest> testUnits = progressHandler
					.performOperation(getDialogType(c));
			if (progressHandler.getUserCancelled()) {
				return;
			}
			progressHandler = null;
			Thread.sleep(1000);
			runTestPhaseStarter.doExecute(javaProject, c, testUnits);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private Boolean getDialogType(final CommitOperation c) {
		return c != null;
	}

}
